package demo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 使用read对文件进行读取，以十六进制方式输出
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2024-03-27  18:41
 */
public class IODemo7 {
    public static void main(String[] args) throws FileNotFoundException {
        try(InputStream inputStream = new FileInputStream("./text.txt")) {  //try()  --> 实现closable接口，自动调用close方法
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);   //以数组的方式读入，返回读入的字节数
                if(n == -1) {
                    break;
                }
                for(int i = 0; i < n; i++) {
                    System.out.printf("%x ",buffer[i]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
